package com.barray.log_monitor;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import java.util.Arrays;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class LogTransactionActivity extends AppCompatActivity {
    private static CheckLogPermissionCallBack mCallback;
    public static boolean hasRegister = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogcatHelper.getInstance(this).start();
        addButtonView();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == LogButtonManager.SETTING_REQUEST_CODE){
            addButtonView();
        }
    }

    private final String [] NO_SHOW_PAGE_LIST = {
            LogPageActivity.class.getSimpleName(),
            LogTransactionActivity.class.getSimpleName(),
            LogDetailActivity.class.getSimpleName()
    };


    private void addButtonView(){
        if(!hasRegister){
            hasRegister = true;
            getApplication().registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
                @Override
                public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {

                }

                @Override
                public void onActivityStarted(@NonNull Activity activity) {

                }

                @Override
                public void onActivityResumed(@NonNull Activity activity) {
                    List<String> pages = Arrays.asList(NO_SHOW_PAGE_LIST);
                    if(pages.contains(activity.getClass().getSimpleName())){
                        LogButtonManager.getInstance().removeButtonView();
                    }else{
                        LogButtonManager.getInstance().addButtonView(activity);
                    }
                }

                @Override
                public void onActivityPaused(@NonNull Activity activity) {

                }

                @Override
                public void onActivityStopped(@NonNull Activity activity) {

                }

                @Override
                public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

                }

                @Override
                public void onActivityDestroyed(@NonNull Activity activity) {
                    if(!isAppRunning(activity)){
                        LogButtonManager.getInstance().removeButtonView();
                    }
                }
            });
        }


        boolean isSuccess = LogButtonManager.getInstance().addButtonView(this);
        if(isSuccess){
            finish();
            if(mCallback != null){
                mCallback.callback();
            }
        }
    }

    //只需要获取当前的上下文，即可判断应用是否在前台
    public static boolean isAppRunning(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> list = activityManager.getRunningTasks(1);
        if(list.size() <= 0) {
            return false;
        }
        if(list.get(0).topActivity.getPackageName().equals(context.getPackageName()) ) {
            return true;
        } else {
            return false;
        }
    }




    public static void start(Context context,CheckLogPermissionCallBack callBack){
        mCallback = callBack;
        Intent intent = new Intent(context,LogTransactionActivity.class);
        context.startActivity(intent);
    }

    public interface CheckLogPermissionCallBack{
        void callback();
    }
}
